﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class ZhaoLuJiHuaTitleWindow : ChildWindow
    {
        private int Year;
        private ZhaoLuService.ZhaoLuServiceClient Client = new ZhaoLuServiceClient();
        public int ID;
        public bool IsNew = false;
        ZhaoLuJiHuaModel model = new ZhaoLuJiHuaModel();
        public ZhaoLuJiHuaTitleWindow(int year, int iD, bool isnew)
        {
            InitializeComponent();
            Year = year;
            ID = iD;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增招录计划";
                model = new ZhaoLuJiHuaModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑招录计划信息";
                Client.GetZhaoLuJiHuaModelCompleted += new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client_GetZhaoLuJiHuaModelCompleted);
                Client.GetZhaoLuJiHuaModelAsync(ID, "",0);
            }
        }

        void Client_GetZhaoLuJiHuaModelCompleted(object sender, GetZhaoLuJiHuaModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            model.Year = Year;
            Client.InsertZhaoLuJiHuaTitleCompleted += new EventHandler<InsertZhaoLuJiHuaTitleCompletedEventArgs>(Client_InsertZhaoLuJiHuaTitleCompleted);
            Client.InsertZhaoLuJiHuaTitleAsync(model);
        }

        private int ShengChengID = 0;
        void Client_InsertZhaoLuJiHuaTitleCompleted(object sender, InsertZhaoLuJiHuaTitleCompletedEventArgs e)
        {
            if (e.Result != 0)
            {
                //ShengChengID = e.Result;
                //ZhaoLuJiHuaWindow win = new ZhaoLuJiHuaWindow(ShengChengID, e.Result, Year, zhiwei.Text, false);
                //win.Show();
                //win.Closed += new EventHandler(win_Closed);
                //this.Visibility = System.Windows.Visibility.Collapsed;

                Client.InsertZhaoLuJiHuaCompleted -= new EventHandler<InsertZhaoLuJiHuaCompletedEventArgs>(Client_InsertZhaoLuJiHuaCompleted);
                Client.InsertZhaoLuJiHuaCompleted += new EventHandler<InsertZhaoLuJiHuaCompletedEventArgs>(Client_InsertZhaoLuJiHuaCompleted);
                Client.InsertZhaoLuJiHuaAsync(e.Result, new ZhaoLuJiHuaDetailModel { ZhaoLuJiHuaID = e.Result }, null);
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }

        }

        void Client_InsertZhaoLuJiHuaCompleted(object sender, InsertZhaoLuJiHuaCompletedEventArgs e)
        {

        }

        void win_Closed(object sender, EventArgs e)
        {
            if (((ZhaoLuJiHuaWindow)sender).DialogResult == true)
                this.DialogResult = true;
            else
            {
                Client.DeleteZhaoLuJiHuaCompleted -= new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                Client.DeleteZhaoLuJiHuaCompleted += new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                Client.DeleteZhaoLuJiHuaAsync(ShengChengID);
            }
        }

        void Client_DeleteZhaoLuJiHuaCompleted(object sender, DeleteZhaoLuJiHuaCompletedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}
